<!DOCTYPE html>
<html lang="zh" xmlns:th="http://www.thymeleaf.org" xmlns:shiro="http://www.pollix.at/thymeleaf/shiro">
    <head>
        <th:block th:include="include :: header('Request列表')" />
    </head>
    <body class="gray-bg">
        <div class="container-div">
            <div class="row">
                <div class="col-sm-12 search-collapse">
                    <form id="formId">
                        <div class="select-list">
                            <ul>
                                <li>
                                    <label>名称：</label>
                                    <input type="text" name="requestName"/>
                                </li>
                                <li>
                                    <label>状态：</label>
                                    <select name="requestStatus" th:with="type=${@dict.getType('sys_request_status')}">
                                        <option value="">所有</option>
                                        <option th:each="dict : ${type}" th:text="${dict.dictLabel}" th:value="${dict.dictValue}"></option>
                                    </select>
                                </li>
                                <li class="select-time">
                                    <label>申请时间： </label>
                                    <input type="text" class="time-input" id="startTime" placeholder="开始时间" name="params[beginTime]"/>
                                    <span>-</span>
                                    <input type="text" class="time-input" id="endTime" placeholder="结束时间" name="params[endTime]"/>
                                </li>
                                <li>
                                    <a class="btn btn-primary btn-rounded btn-sm" onclick="$.table.search()"><i class="fa fa-search"></i>&nbsp;搜索</a>
                                    <a class="btn btn-warning btn-rounded btn-sm" onclick="$.form.reset()"><i class="fa fa-refresh"></i>&nbsp;重置</a>
                                </li>
                            </ul>
                        </div>
                    </form>
                </div>

                <div class="btn-group-sm" id="toolbar" role="group">
                    <a class="btn btn-success btn-outline" onclick="$.operate.addTab()" shiro:hasPermission="system:request:add">
                        <i class="fa fa-plus"></i> 新增
                    </a>
                    <a class="btn btn-primary btn-outline" onclick="add2()" shiro:hasPermission="system:request:edit">
                        <i class="fa fa-edit"></i> 新增2
                    </a>
                    <a class="btn btn-warning btn-outline" onclick="$.table.exportExcel()" shiro:hasPermission="system:request:export">
                        <i class="fa fa-download"></i> 导出
                    </a>
                </div>
                <div class="col-sm-12 select-table table-striped">
                    <table id="bootstrap-table"></table>
                </div>
            </div>
        </div>
        <th:block th:include="include :: footer" />
        <script th:inline="javascript">
            var editFlag = [[${@permission.hasPermi('system:request:edit')}]];
            var removeFlag = [[${@permission.hasPermi('system:request:remove')}]];
            var requestStatusDatas = [[${@dict.getType('sys_request_status')}]];
            var prefix = ctx + "system/request";
            var downloadUrl = prefix + "/download";

            $(function() {
                var options = {
                url: prefix + "/list",
                viewUrl2: prefix + "/samview/{id}",
                viewUrl3: prefix + "/serview/{id}",
                detailUrl: prefix + "/detail/{id}",
                createUrl: prefix + "/add",
                updateUrl: prefix + "/edit/{id}",
                removeUrl: prefix + "/remove",
                cancelUrl: prefix + "/cancel",
                exportUrl: prefix + "/export",
                modalName: "Request",
                sortName: "createTime",
                sortOrder: "desc",
                onLoadSuccess: function (data){
                    onLoadSuccess(data);
                },
                columns: [{
                    checkbox: true
                },
                {
                    field: 'requestId',
                    title: '主键',
                    visible: false
                },
                {
                    field: 'requestName',
                    title: '服务请求名称',
                    formatter: function (value, row, index) {
                        return '<a href="javascript:void(0)" onclick="$.operate.detailTab2(\'' + row.requestId + '\')">'+value+'</a>';
                    }
                },
                {
                    field: 'samples'  ,
                    title: '样品',
                    formatter: function (value, item, index) {
                        var samples = [];
                        $.each(value,function (index, data) {
                            var s = JSON.stringify(data);
                            var parse = JSON.parse(s);
                            samples.push('<a href="javascript:;" onclick="$.operate.view2(\''+parse.sampleId+'\',\'样品\')"><i class="fa fa-flask"></i> '+parse.sampleName+'</a><br>')
                        })
                        return samples.join('');
                    }
                },
                {
                    field: 'services'  ,
                    title: '服务',
                    formatter: function (value, item, index) {
                        var services = [];
                        $.each(value,function (index, data) {
                            var s = JSON.stringify(data);
                            var parse = JSON.parse(s);
                            services.push('<a id="serv'+parse.serviceId+'" href="javascript:;" onclick="$.operate.view3(\''+parse.serviceId+'\',\'服务\')"><i class="fa fa-adjust fa-lg"></i> '+parse.serviceName+' - '+parse.sampleName+'</a><br>')
                        })
                        return services.join('');
                    }
                },
                {
                    field: 'description',
                    title: '描述'
                },
                {
                    field: 'requestStatus',
                    title: '状态',
                    formatter: function(value, row, index) {
                        return $.table.selectDictLabel(requestStatusDatas, value);
                    }
                },
                {
                    field: 'createTime',
                    title: '申请时间',
                    sortable: true
                },
                {
                    field: 'completeness',
                    title: '完成度',
                    visible: false
                },
                {
                    field: 'userReq.userName',
                    title: '创建者'
                },
                {
                    field: 'userMag.userName',
                    title: 'AM',
                    visible: false
                },
                {
                    field: 'userApr.userName',
                    title: 'QA',
                    visible: false
                },
                {
                    field: 'result',
                    title: '结果',
                    formatter: function(value, row, index) {
                        if(row.requestStatus == 8 && value != null){
                            return '<a href="'+downloadUrl+'?id='+row.requestId+'"><i class="fa fa-download"></i> '+row.requestName+'</a>';
                        }
                    }
                },
                {
                    title: '操作',
                    align: 'center',
                    formatter: function(value, row, index) {
                        var actions = [];
                        var remove = '<a class="btn btn-danger btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.remove(\'' + row.requestId + '\')"><i class="fa fa-remove"></i>删除</a>';
                        var edit = '<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="$.operate.edit(\'' + row.requestId + '\')"><i class="fa fa-edit"></i>编辑</a> ';
                        var cancel = '<a class="btn btn-warning btn-xs ' + removeFlag + '" href="javascript:void(0)" onclick="$.operate.cancel(\'' + row.requestId + '\')"><i class="fa fa-remove"></i>取消</a>';
                        var reSubmit = '<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick="reSubmit(\'' + row.requestId + '\')"><i class="fa fa-edit"></i>提交</a> ';
                        // var reSubmit = '<a class="btn btn-success btn-xs ' + editFlag + '" href="javascript:void(0)" onclick=""><i class="fa fa-edit"></i>提交</a> ';
                        if (row.requestStatus == 1 || row.requestStatus == 2){
                            // actions.push(edit);
                            actions.push(remove);
                        }else if(row.requestStatus == 8){
                            actions.push(remove);
                        }else if(row.requestStatus == 11 || row.requestStatus == 4){
                            actions.push(reSubmit);
                            actions.push(remove);
                        }else {
                            // actions.push(edit);
                            actions.push(cancel);
                        }
                        return actions.join('');
                    }
                }]
            };
                $.table.init(options);
            });

            function reSubmit(requestId){
                $.modal.confirm("确定要重新提交Request吗？", function (){
                    $.operate.post(prefix +"/reSubmit", {"requestId": requestId});
                })
            }

            function onLoadSuccess(data){
                $.each(data.rows, function (index, value){
                    if(value.length == 0){
                        return;
                    }
                    $.each(value.services, function( key, value, index) {
                        if(value.process == 11){ //Done: 已完成
                            $("#serv" + value.serviceId +" i:first-child").css("color","green");
                            $("#serv" + value.serviceId +" i:first-child").removeClass();
                            $("#serv" + value.serviceId +" i:first-child").addClass("fa fa-check fa-lg")
                        }else if(value.process == 12 || value.process == 1 ){ //Error、cancel: 出错
                            $("#serv" + value.serviceId +" i:first-child").css("color","red");
                            $("#serv" + value.serviceId +" i:first-child").removeClass();
                            $("#serv" + value.serviceId +" i:first-child").addClass("fa fa-close fa-lg")
                        }else if(value.process == 2){   //Submitted: 提交等待
                            // $("#serv" + value.serviceId +" i:first-child").css("color","blue");
                            $("#serv" + value.serviceId +" i:first-child").removeClass();
                            $("#serv" + value.serviceId +" i:first-child").addClass("fa fa-hourglass-1 fa-lg")
                        }else if(value.process != null && value.process != 0){ //Queued、temp、shim、...: 进行中
                            // $("#serv" + value.serviceId +" i:first-child").css("color","blue");
                            $("#serv" + value.serviceId +" i:first-child").removeClass();
                            $("#serv" + value.serviceId +" i:first-child").addClass("fa fa-spin fa-circle-o-notch fa-lg")
                        }

                    });
                });
            }

            function add2() {
                $.modal.openTab("Add Request", ctx + "system/request2/add");
            }
        </script>
    </body>
</html>